/*
 * @Description: 
 * @Autor: ilmari
 * @Date: 2021-05-18 09:32:28
 * @LastEditors: ilmari
 * @LastEditTime: 2021-05-18 10:23:39
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:rcmd/app/modules/rcmd/controllers/ip_cluster_cfg_controller.dart';
import 'package:rcmd/app/modules/rcmd/controllers/rcmd_controller.dart';
import 'package:rcmd/app/utils/extensions/extension_sizebox.dart';

import 'persist_page.dart';


class IpClusterCfgPage extends PersistPage<IpClusterCfgController> {
  IpClusterCfgPage(String key) : super(key, IpClusterCfgController());

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Expanded(child:outline(Obx(() => ListView.builder(
                  itemCount: controller.ipCount.value,
                  itemBuilder: ipItem,
                  padding: EdgeInsets.only(bottom: 10)
                )), label: "IP列表", 
                rightTop: IconButton(onPressed: (){_addIp(context);}, 
                icon: Icon(Icons.new_label, color: Colors.lightBlue,)))),
        Expanded(child: outline(Obx(() => ListView.builder(
                  itemCount: controller.clusterCount.value,
                  itemBuilder: clusterItem
                )), label:"集群列表",
                rightTop: IconButton(onPressed: (){_addCluster(context);}, 
                icon: Icon(Icons.new_label, color: Colors.lightBlue,))))
      
      ],
    );
  }

  Widget itemList(Widget Function(BuildContext context, int index)build ) {
    return ListView.builder(
              itemCount: controller.ipCount.value,
              itemBuilder: build,
              padding: EdgeInsets.only(bottom: 10)
            );
  }

  Widget ipItem(BuildContext context, int index) {
    final text = controller.allIpListNotInCluster.elementAt(index);
    return textItem(context, text, () {
      controller.deleteIp(text);
    }, (){
      Get.find<RcmdController>().addCustomCmdPage(selIpList:[text]);
    });
  }

  Widget clusterItem(BuildContext context, int index) {
    final text = controller.allClusterList.elementAt(index);
    return textItem(context, text, () {
      controller.deleteCluster(text);
    }, (){
      Get.find<RcmdController>().addCustomCmdPage(selIpList:controller.getIpListByCluster(text));
    });
  }

  Widget textItem(BuildContext context, String text, Function() onPress, Function() onDoubleTap) {
    return 
      ButtonBar(
        alignment: MainAxisAlignment.spaceBetween,
        mainAxisSize: MainAxisSize.max,
        buttonPadding: EdgeInsets.all(0),
        children: [
          GestureDetector(child: outline(Text(text), color: Colors.transparent, padding: EdgeInsets.only(left: 10, right: 200), margin: EdgeInsets.zero), onDoubleTap: onDoubleTap),
          IconButton(onPressed: onPress, icon: Icon(Icons.delete, color: Colors.redAccent,))])
      ;
  }

  void _addIp(BuildContext context) {
    _addSshCfg(context, false, (ip, port, user, pswd, name) { 
      controller.updateSshCfg(ip, user, pswd, port);
    });
  }

  void _addCluster(BuildContext context) {
    _addSshCfg(context, true, (ip, port, user, pswd, cluster) { 
      controller.addCluster(cluster, ip, user, pswd, port);
    });
  }

  void _addSshCfg(BuildContext context, bool needName, void Function(String ip, int port, String user, String pswd, String name) onSubmit) {
    var ip = "172.24.37.";
    var port = "9622";
    var user = "root";
    var pswd = "ruijie1688";
    var name = "";
    FocusScopeNode _scopeNode = FocusScopeNode();
    final _label = (text)=>SizedBox(child: Text(text), width: 100,);
    final _children = [
      ListTile(leading: _label("IP:"), title: textField(ip, (val, over) {
        ip = val;
        if (over) _scopeNode.nextFocus();
      })),
      ListTile(leading: _label("Port:"), title: textField(port, (val, over) {
        port = val;
        if (over) _scopeNode.nextFocus();
      })),
      ListTile(leading: _label("User:"), title: textField(user, (val, over) {
        user = val;
        if (over) _scopeNode.nextFocus();
      })),
      ListTile(leading: _label("Password:"), title: textField(pswd, (val, over) {
        pswd = val;
        if (over) _scopeNode.nextFocus();
      })),
      OutlinedButton(onPressed: (){
        if (ip.trim().isEmpty)    return ;
        if (user.trim().isEmpty)    return ;
        if (pswd.trim().isEmpty)    return ;
        try {
          onSubmit(ip, int.parse(port), user, pswd, name);
          Navigator.of(context).pop();
        } catch (e) {
        }
      }, child: Text("添加"), 
        style: ButtonStyle(
          padding: MaterialStateProperty.all(EdgeInsets.symmetric( vertical: 20, horizontal: 0)))),
    ]; 
    if (needName) {
      _children.insert(0, ListTile(leading: _label("名字:"), title: textField(ip, (val, over) {
        name = val;
        if (over) _scopeNode.nextFocus();
      })));
    }
    Get.bottomSheet(
      outline(
      FocusScope(
        node: _scopeNode,
        child: ListView(
          children: _children.map((e) => padding(e)).toList(),
          shrinkWrap: true,
        )
      )),
    barrierColor: Colors.transparent,
    backgroundColor: Colors.white);
  }
}
